<?php
defined('_JEXEC') or die();

class WrdControllerDiscount extends JControllerLegacy 
{
        protected $option;
        protected $controller; 
	function __construct( $default = array()){
		$this->option = 'com_wrd';
		$this->controller = 'discount';
		parent::__construct( $default );
	}

	function display()
	{
		switch($this->getTask()){
			case 'add':{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view'  , 'discount');
			} 
					break;
	            default:
					JRequest::setVar( 'view'  , 'discount');
	            	break;
			}
			parent::display();
        }
        
      //save profile info
    function save(){
	   $post=JRequest::get('post');
       $post['created_date'] = date("Y-m-d H:i:s");
       $post['o_code_tmp'] = serialize($post['o_code']);
       unset($post['o_code']);
       $post['o_code'] = $post['o_code_tmp'];
       unset($post['o_code_tmp']);
       $params = JComponentHelper::getParams('com_wrd');
       $model = $this->getModel('discount');
       
       if($post['rule'] != 5)
       		$post['lucky'] = '';
       else{
       		$luckynum_arr = explode(',', $post['lucky']);
       		foreach ($luckynum_arr as $num=>$val){
       			$luckynum_arr[$num] = trim($val);
       		}
       		
       		$post['lucky'] = serialize($luckynum_arr);
       }

		if($post['rule'] != 3){
			$post['ordinary'] = '';
		}
		
		if($post['rule'] != 4){
			$post['multiples'] = '';
		}
       
       //get date current  
       $outlet = $this->getModel("outlets");
       $id_category = $model->store($post,'discount',$outlet->getList());
	   
       if($id_category != 0){
       		//send email notice
			WrdHelper::sendEmailNotice($post);
			
            $this->setRedirect('index.php?option=com_wrd&controller=discount','Saved');
            return true;                
       }
       else{
            $this->setRedirect('index.php?option=com_wrd&controller=discount','Error Saved');
            return false;
       }
    }    
   
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('discount');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=discount' );
	}
   
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}

		$model = $this->getModel('discount');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=discount' );
	}

	function unpublish(){
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}

		$model = $this->getModel('discount');
		
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect( 'index.php?option=com_wrd&controller=discount' );
	}
}
?>